package com.unswift.window.table;

import java.util.HashMap;
import java.util.Map;

import javax.swing.JComponent;
import javax.swing.JTable;

@SuppressWarnings("serial")
public class Table extends JTable{
	
	private Map<String, Object> attribute;//存储表格关联的对象，见ATTRIBUTE_开头的常量
	private String busAction;//表格动作，什么类型的表格
	private TableTitle[] columnNames;
	private JComponent[] editorComps;//编辑表格的对象，初始化表格时新建CellEditor对象里包含的对象，可再定位某个单元格后直接让他获取焦点，其它方式获取焦点暂时没研究出来
	
	public Table(TableModel model, String busAction) {
		super(model);
		this.busAction=busAction;
		this.attribute=new HashMap<String, Object>();
		this.columnNames=model.getColumns();
		
		this.editorComps=new JComponent[model.getColumns().length];
//		InputMap inputMap = this.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
//		rewriteKeyboard(inputMap);
	}

//	private void rewriteKeyboard(InputMap inputMap) {
//		//重写Ctrl+C
//		MenuEnum copy = MenuEnum.COPY;
//		inputMap.put(copy.getStroke(), "none");
//		this.registerKeyboardAction(new MenuActionListener(this, -1, -1), copy.getAction(), copy.getStroke(), JComponent.WHEN_FOCUSED);
//		//重写Ctrl+V
//		MenuEnum stick = MenuEnum.STICK;
//		inputMap.put(stick.getStroke(), "none");
//		this.registerKeyboardAction(new MenuActionListener(this, -1, -1), stick.getAction(), stick.getStroke(), JComponent.WHEN_FOCUSED);
//		//重写Ctrl+S
//		MenuEnum save = MenuEnum.SAVE;
//		inputMap.put(save.getStroke(), "none");
//		this.registerKeyboardAction(new MenuActionListener(this, -1, -1), save.getAction(), save.getStroke(), JComponent.WHEN_FOCUSED);
//		//重写Tab
//		MenuEnum tableTab=MenuEnum.TABLE_TAB;
//		inputMap.put(tableTab.getStroke(), "none");
//		this.registerKeyboardAction(new MenuActionListener(this, -1, -1), tableTab.getAction(), tableTab.getStroke(), JComponent.WHEN_FOCUSED);
//		//重写Enter
//		MenuEnum tableEnter=MenuEnum.TABLE_ENTER;
//		inputMap.put(tableEnter.getStroke(), "none");
//		this.registerKeyboardAction(new MenuActionListener(this, -1, -1), tableEnter.getAction(), tableEnter.getStroke(), JComponent.WHEN_FOCUSED);
//	}
	
	@SuppressWarnings("unchecked")
	public <T> T getAttribute(String key) {
		return (T)attribute.get(key);
	}
	public <T> void setAttribute(String key, T value) {
		this.attribute.put(key, value);
	}
	public boolean existsAttribute(String key) {
		return attribute.containsKey(key);
	}
	public void removeAttribute(String key) {
		this.attribute.remove(key);
	}
	public String getBusAction() {
		return busAction;
	}
	public void setBusAction(String busAction) {
		this.busAction = busAction;
	}
	public TableTitle[] getColumnTitles() {
		return columnNames;
	}
	public TableTitle getColumnTitle(int column) {
		return this.columnNames[column];
	}
	public boolean getColumnTitleEditor() {
		for (TableTitle tableTitle : columnNames) {
			if(tableTitle.isEditor()){
				return true;
			}
		}
		return false;
	}
	/**
	 * 设置编辑表格的对象，初始化表格时新建CellEditor对象里包含的对象，可再定位某个单元格后直接让他获取焦点，其它方式获取焦点暂时没研究出来
	 * @param index
	 * @param comp
	 */
	public void setEditorComp(int index, JComponent comp){
		editorComps[index]=comp;
	}
	
	@SuppressWarnings("unchecked")
	public <T extends JComponent> T getEditorComp(int index) {
		return (T)editorComps[index];
	}

	/**
	 * 获取单元格key
	 * @param row
	 * @param column
	 * @return
	 */
	public String getCellKey(int row, int column) {
		return row+":"+column;
	}
	
}
